# -*- coding: utf-8 -*-
from datetime import timedelta

from jms.dwd.dwd_warhouse.dwd_wide_rank_basic_scaninfo_tms_dt import jms_dwd__dwd_wide_rank_basic_scaninfo_tms_dt_0110
from jms.dwd.dwd_warhouse.dwd_wide_rank_basic_scaninfo_tms_dt import jms_dwd__dwd_wide_rank_basic_scaninfo_tms_dt_1120
from jms.dwd.dwd_warhouse.dwd_wide_rank_basic_scaninfo_tms_dt import jms_dwd__dwd_wide_rank_basic_scaninfo_tms_dt_2130
from jms.dwd.dwd_warhouse.dwd_wide_rank_basic_scaninfo_tms_dt import jms_dwd__dwd_wide_rank_basic_scaninfo_tms_dt_3140
from jms.dwd.dwd_warhouse.dwd_wide_rank_basic_scaninfo_tms_dt import jms_dwd__dwd_wide_rank_basic_scaninfo_tms_dt_4150
from jms.dwd.dwd_warhouse.dwd_wide_rank_basic_scaninfo_tms_dt import jms_dwd__dwd_wide_rank_basic_scaninfo_tms_dt_2049
from jms.dwd.dwd_wide_abnormal_detail_waybill_dt import jms_dwd__dwd_wide_abnormal_detail_waybill_dt
from jms.dwd.oms.dwd_yl_oms_oms_waybill_incre_dt import jms_dwd__dwd_yl_oms_oms_waybill_incre_dt
from jms.dwd.oms.dwd_oms_order_dt import jms_dwd__dwd_oms_order_dt
from jms.dwd.oms.dwd_yl_oms_oms_order_incre_dt import jms_dwd__dwd_yl_oms_oms_order_incre_dt
from utils.operators.cluster_for_spark_sql_operator import SparkSqlOperator
from airflow.operators.dummy_operator import DummyOperator

jms_dwd__dwd_wide_summary_waybill_0110 = SparkSqlOperator(
    task_id='jms_dwd__dwd_wide_summary_waybill_0110',
    task_concurrency=1,
    pool_slots=12,
    master='yarn',
    name='jms_dwd__dwd_wide_summary_waybill_0110_{{ execution_date | date_add(1) | cst_ds }}',
    sql='jms/dwd/dwd_warhouse/dwd_wide_summary_waybill/execute_0110.sql',
    driver_memory='20G',
    driver_cores=8,
    retries=1,
    email=['rongguangfan@jtexpress.com','yl_bigdata@yl-scm.com'],
    executor_cores=20,
    executor_memory='32G',
    num_executors=60,  # spark.dynamicAllocation.enabled 为 True 时，num_executors 表示最少 Executor 数
    conf={'spark.dynamicAllocation.enabled': 'true',  # 动态资源开启
          'spark.shuffle.service.enabled': 'true',  # 动态资源 Shuffle 服务开启
          'spark.dynamicAllocation.maxExecutors': 65,  # 动态资源最大扩容 Executor 数
          'spark.dynamicAllocation.cachedExecutorIdleTimeout': 180,  # 动态资源自动释放闲置 Executor 的超时时间(s)
          'spark.sql.sources.partitionOverwriteMode': 'dynamic',  # 允许删改已存在的分区
          'spark.executor.memoryOverhead': '4G',  # 堆外内存
          'spark.sql.shuffle.partitions': 3000,
          'spark.default.paralleism': 3000,
          'spark.hadoop.hive.exec.dynamic.partition.mode': 'true',
          'spark.shuffle.file.buffer': '48KB',
          'spark.reducer.maxSizeInFlight': '72MB',
          # 'spark.sql.autoBroadcastJoinThreshold': 3221225472
    },
    # hiveconf={'hive.exec.dynamic.partition': 'true',  # 动态分区
    #           'hive.exec.dynamic.partition.mode': 'nonstrict',
    #           'hive.exec.max.dynamic.partitions': 1800,  # 每天生成 60 个分区
    #           'hive.exec.max.dynamic.partitions.pernode': 180,  # 每天生成 60 个分区
    #           },
    yarn_queue='pro',
    execution_timeout=timedelta(minutes=90),
)

jms_dwd__dwd_wide_summary_waybill_1120 = SparkSqlOperator(
    task_id='jms_dwd__dwd_wide_summary_waybill_1120',
    task_concurrency=1,
    pool_slots=12,
    master='yarn',
    name='jms_dwd__dwd_wide_summary_waybill_1120_{{ execution_date | date_add(1) | cst_ds }}',
    sql='jms/dwd/dwd_warhouse/dwd_wide_summary_waybill/execute_1120.sql',
    driver_memory='20G',
    driver_cores=8,
    retries=1,
    email=['rongguangfan@jtexpress.com','yl_bigdata@yl-scm.com'],
    executor_cores=20,
    executor_memory='32G',
    num_executors=60,  # spark.dynamicAllocation.enabled 为 True 时，num_executors 表示最少 Executor 数
    conf={'spark.dynamicAllocation.enabled': 'true',  # 动态资源开启
          'spark.shuffle.service.enabled': 'true',  # 动态资源 Shuffle 服务开启
          'spark.dynamicAllocation.maxExecutors': 65,  # 动态资源最大扩容 Executor 数
          'spark.dynamicAllocation.cachedExecutorIdleTimeout': 180,  # 动态资源自动释放闲置 Executor 的超时时间(s)
          'spark.sql.sources.partitionOverwriteMode': 'dynamic',  # 允许删改已存在的分区
          'spark.executor.memoryOverhead': '4G',  # 堆外内存
          'spark.sql.shuffle.partitions': 3000,
          'spark.default.paralleism': 3000,
          'spark.hadoop.hive.exec.dynamic.partition.mode': 'true',
          'spark.shuffle.file.buffer': '48KB',
          'spark.reducer.maxSizeInFlight': '72MB',
          # 'spark.sql.autoBroadcastJoinThreshold': 3221225472
          },
    # hiveconf={'hive.exec.dynamic.partition': 'true',  # 动态分区
    #           'hive.exec.dynamic.partition.mode': 'nonstrict',
    #           'hive.exec.max.dynamic.partitions': 1800,  # 每天生成 60 个分区
    #           'hive.exec.max.dynamic.partitions.pernode': 180,  # 每天生成 60 个分区
    #           },
    yarn_queue='pro',
    execution_timeout=timedelta(minutes=90),
)

jms_dwd__dwd_wide_summary_waybill_2130 = SparkSqlOperator(
    task_id='jms_dwd__dwd_wide_summary_waybill_2130',
    task_concurrency=1,
    pool_slots=12,
    master='yarn',
    name='jms_dwd__dwd_wide_summary_waybill_2130_{{ execution_date | date_add(1) | cst_ds }}',
    sql='jms/dwd/dwd_warhouse/dwd_wide_summary_waybill/execute_2130.sql',
    driver_memory='20G',
    driver_cores=8,
    retries=1,
    email=['rongguangfan@jtexpress.com','yl_bigdata@yl-scm.com'],
    executor_cores=20,
    executor_memory='32G',
    num_executors=60,  # spark.dynamicAllocation.enabled 为 True 时，num_executors 表示最少 Executor 数
    conf={'spark.dynamicAllocation.enabled': 'true',  # 动态资源开启
          'spark.shuffle.service.enabled': 'true',  # 动态资源 Shuffle 服务开启
          'spark.dynamicAllocation.maxExecutors': 65,  # 动态资源最大扩容 Executor 数
          'spark.dynamicAllocation.cachedExecutorIdleTimeout': 180,  # 动态资源自动释放闲置 Executor 的超时时间(s)
          'spark.sql.sources.partitionOverwriteMode': 'dynamic',  # 允许删改已存在的分区
          'spark.executor.memoryOverhead': '4G',  # 堆外内存
          'spark.sql.shuffle.partitions': 3000,
          'spark.default.paralleism': 3000,
          'spark.hadoop.hive.exec.dynamic.partition.mode': 'true',
          'spark.shuffle.file.buffer': '48KB',
          'spark.reducer.maxSizeInFlight': '72MB',
          # 'spark.sql.autoBroadcastJoinThreshold': 3221225472
          },
    # hiveconf={'hive.exec.dynamic.partition': 'true',  # 动态分区
    #           'hive.exec.dynamic.partition.mode': 'nonstrict',
    #           'hive.exec.max.dynamic.partitions': 1800,  # 每天生成 60 个分区
    #           'hive.exec.max.dynamic.partitions.pernode': 180,  # 每天生成 60 个分区
    #           },
    yarn_queue='pro',
    execution_timeout=timedelta(minutes=90),
)

jms_dwd__dwd_wide_summary_waybill_3140 = SparkSqlOperator(
    task_id='jms_dwd__dwd_wide_summary_waybill_3140',
    task_concurrency=1,
    pool_slots=12,
    master='yarn',
    name='jms_dwd__dwd_wide_summary_waybill_3140_{{ execution_date | date_add(1) | cst_ds }}',
    sql='jms/dwd/dwd_warhouse/dwd_wide_summary_waybill/execute_3140.sql',
    driver_memory='20G',
    driver_cores=8,
    retries=1,
    email=['rongguangfan@jtexpress.com','yl_bigdata@yl-scm.com'],
    executor_cores=20,
    executor_memory='32G',
    num_executors=60,  # spark.dynamicAllocation.enabled 为 True 时，num_executors 表示最少 Executor 数
    conf={'spark.dynamicAllocation.enabled': 'true',  # 动态资源开启
          'spark.shuffle.service.enabled': 'true',  # 动态资源 Shuffle 服务开启
          'spark.dynamicAllocation.maxExecutors': 65,  # 动态资源最大扩容 Executor 数
          'spark.dynamicAllocation.cachedExecutorIdleTimeout': 180,  # 动态资源自动释放闲置 Executor 的超时时间(s)
          'spark.sql.sources.partitionOverwriteMode': 'dynamic',  # 允许删改已存在的分区
          'spark.executor.memoryOverhead': '4G',  # 堆外内存
          'spark.sql.shuffle.partitions': 3000,
          'spark.default.paralleism': 3000,
          'spark.hadoop.hive.exec.dynamic.partition.mode': 'true',
          'spark.shuffle.file.buffer': '48KB',
          'spark.reducer.maxSizeInFlight': '72MB',
          # 'spark.sql.autoBroadcastJoinThreshold': 3221225472
          },
    # hiveconf={'hive.exec.dynamic.partition': 'true',  # 动态分区
    #           'hive.exec.dynamic.partition.mode': 'nonstrict',
    #           'hive.exec.max.dynamic.partitions': 1800,  # 每天生成 60 个分区
    #           'hive.exec.max.dynamic.partitions.pernode': 180,  # 每天生成 60 个分区
    #           },
    yarn_queue='pro',
    execution_timeout=timedelta(minutes=90),
)

jms_dwd__dwd_wide_summary_waybill_4150 = SparkSqlOperator(
    task_id='jms_dwd__dwd_wide_summary_waybill_4150',
    task_concurrency=1,
    pool_slots=12,
    master='yarn',
    name='jms_dwd__dwd_wide_summary_waybill_4150_{{ execution_date | date_add(1) | cst_ds }}',
    sql='jms/dwd/dwd_warhouse/dwd_wide_summary_waybill/execute_4150.sql',
    driver_memory='10G',
    driver_cores=4,
    retries=1,
    email=['rongguangfan@jtexpress.com','yl_bigdata@yl-scm.com'],
    executor_cores=20,
    executor_memory='32G',
    num_executors=30,  # spark.dynamicAllocation.enabled 为 True 时，num_executors 表示最少 Executor 数
    conf={'spark.dynamicAllocation.enabled': 'true',  # 动态资源开启
          'spark.shuffle.service.enabled': 'true',  # 动态资源 Shuffle 服务开启
          'spark.dynamicAllocation.maxExecutors': 35,  # 动态资源最大扩容 Executor 数
          'spark.dynamicAllocation.cachedExecutorIdleTimeout': 180,  # 动态资源自动释放闲置 Executor 的超时时间(s)
          'spark.sql.sources.partitionOverwriteMode': 'dynamic',  # 允许删改已存在的分区
          'spark.executor.memoryOverhead': '4G',  # 堆外内存
          'spark.sql.shuffle.partitions': 3000,
          'spark.default.paralleism': 3000,
          'spark.hadoop.hive.exec.dynamic.partition.mode': 'true',
          'spark.shuffle.file.buffer': '48KB',
          'spark.reducer.maxSizeInFlight': '72MB',
          # 'spark.sql.autoBroadcastJoinThreshold': 3221225472
          },
    # hiveconf={'hive.exec.dynamic.partition': 'true',  # 动态分区
    #           'hive.exec.dynamic.partition.mode': 'nonstrict',
    #           'hive.exec.max.dynamic.partitions': 1800,  # 每天生成 60 个分区
    #           'hive.exec.max.dynamic.partitions.pernode': 180,  # 每天生成 60 个分区
    #           },
    yarn_queue='pro',
    execution_timeout=timedelta(minutes=60),
)

jms_dwd__dwd_wide_summary_waybill_2049 = SparkSqlOperator(
    task_id='jms_dwd__dwd_wide_summary_waybill_2049',
    task_concurrency=1,
    pool_slots=12,
    master='yarn',
    name='jms_dwd__dwd_wide_summary_waybill_2049_{{ execution_date | date_add(1) | cst_ds }}',
    sql='jms/dwd/dwd_warhouse/dwd_wide_summary_waybill/execute_2049.sql',
    driver_memory='12G',
    driver_cores=4,
    retries=1,
    email=['rongguangfan@jtexpress.com','yl_bigdata@yl-scm.com'],
    executor_cores=4,
    executor_memory='20G',
    num_executors=20,  # spark.dynamicAllocation.enabled 为 True 时，num_executors 表示最少 Executor 数
    conf={'spark.dynamicAllocation.enabled': 'true',  # 动态资源开启
          'spark.shuffle.service.enabled': 'true',  # 动态资源 Shuffle 服务开启
          'spark.dynamicAllocation.maxExecutors': 55,  # 动态资源最大扩容 Executor 数
          'spark.dynamicAllocation.cachedExecutorIdleTimeout': 180,  # 动态资源自动释放闲置 Executor 的超时时间(s)
          'spark.sql.sources.partitionOverwriteMode': 'dynamic',  # 允许删改已存在的分区
          'spark.executor.memoryOverhead': '4G',  # 堆外内存
          'spark.sql.shuffle.partitions': 3000,
          'spark.default.paralleism': 3000,
          'spark.hadoop.hive.exec.dynamic.partition.mode': 'true',
          'spark.shuffle.file.buffer': '48KB',
          'spark.reducer.maxSizeInFlight': '72MB'
          },
    # hiveconf={'hive.exec.dynamic.partition': 'true',  # 动态分区
    #           'hive.exec.dynamic.partition.mode': 'nonstrict',
    #           'hive.exec.max.dynamic.partitions': 1800,  # 每天生成 60 个分区
    #           'hive.exec.max.dynamic.partitions.pernode': 180,  # 每天生成 60 个分区
    #           },
    yarn_queue='pro',
    execution_timeout=timedelta(minutes=90),
)

jms_dwd__dwd_wide_summary_waybill = DummyOperator(
    task_id='jms_dwd__dwd_wide_summary_waybill',
    email=['rongguangfan@jtexpress.com','yl_bigdata@yl-scm.com'],
    retries=0,
    priority_weight=0,
)

jms_dwd__dwd_wide_summary_waybill_0110 << [
    jms_dwd__dwd_wide_rank_basic_scaninfo_tms_dt_0110
    ,jms_dwd__dwd_wide_abnormal_detail_waybill_dt
    ,jms_dwd__dwd_yl_oms_oms_waybill_incre_dt
    ,jms_dwd__dwd_yl_oms_oms_order_incre_dt
]
jms_dwd__dwd_wide_summary_waybill_1120 << [
    jms_dwd__dwd_wide_rank_basic_scaninfo_tms_dt_1120
    ,jms_dwd__dwd_wide_abnormal_detail_waybill_dt
    ,jms_dwd__dwd_yl_oms_oms_waybill_incre_dt
    ,jms_dwd__dwd_yl_oms_oms_order_incre_dt
]
jms_dwd__dwd_wide_summary_waybill_2130 << [
    jms_dwd__dwd_wide_rank_basic_scaninfo_tms_dt_2130
    ,jms_dwd__dwd_wide_abnormal_detail_waybill_dt
    ,jms_dwd__dwd_yl_oms_oms_waybill_incre_dt
    ,jms_dwd__dwd_yl_oms_oms_order_incre_dt
]
jms_dwd__dwd_wide_summary_waybill_3140 << [
    jms_dwd__dwd_wide_rank_basic_scaninfo_tms_dt_3140
    ,jms_dwd__dwd_wide_abnormal_detail_waybill_dt
    ,jms_dwd__dwd_yl_oms_oms_waybill_incre_dt
    ,jms_dwd__dwd_yl_oms_oms_order_incre_dt
]
jms_dwd__dwd_wide_summary_waybill_4150 << [
    jms_dwd__dwd_wide_rank_basic_scaninfo_tms_dt_4150
    ,jms_dwd__dwd_wide_abnormal_detail_waybill_dt
    ,jms_dwd__dwd_yl_oms_oms_waybill_incre_dt
    ,jms_dwd__dwd_yl_oms_oms_order_incre_dt
]
jms_dwd__dwd_wide_summary_waybill_2049 << [
    jms_dwd__dwd_wide_rank_basic_scaninfo_tms_dt_2049
    ,jms_dwd__dwd_wide_abnormal_detail_waybill_dt
    ,jms_dwd__dwd_yl_oms_oms_waybill_incre_dt
    ,jms_dwd__dwd_yl_oms_oms_order_incre_dt
]
jms_dwd__dwd_wide_summary_waybill << [
    jms_dwd__dwd_wide_summary_waybill_0110,
    jms_dwd__dwd_wide_summary_waybill_1120,
    jms_dwd__dwd_wide_summary_waybill_2130,
    jms_dwd__dwd_wide_summary_waybill_3140,
    jms_dwd__dwd_wide_summary_waybill_4150,
    jms_dwd__dwd_wide_summary_waybill_2049,
]
